package com.jsh.erp.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jsh.erp.constants.BusinessConstants;
import com.jsh.erp.datasource.entities.DailyItem;
import com.jsh.erp.datasource.entities.DepotHead;
import com.jsh.erp.datasource.entities.DepotItemVo4WithInfoEx;
import com.jsh.erp.datasource.entities.Unit;
import com.jsh.erp.service.dailyItem.DailyItemService;
import com.jsh.erp.service.depotItem.DepotItemService;
import com.jsh.erp.service.redis.RedisService;
import com.jsh.erp.utils.BaseResponseInfo;
import com.jsh.erp.utils.ErpInfo;
import com.jsh.erp.utils.StringUtil;
import com.jsh.erp.utils.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static com.jsh.erp.utils.Tools.getCenternTime;

/**
 * @author wy
 * @title: DailyItemController
 * @projectName JSH_ERP
 * @description:
 * @date 2023-01-13 9:50
 */
@RestController
@RequestMapping(value = "/dailyItem")
@Api(tags = {"每日对账子表"})
public class DailyItemController {

    @Resource
    private RedisService redisService;
    @Resource
    private DailyItemService dailyItemService;

    @GetMapping(value = "/getDetailList")
    @ApiOperation(value = "单据明细列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "执行成功",response = DailyItem.class)
    })
    public BaseResponseInfo getDetailList(@RequestParam("headerId") String headerId,
                                          HttpServletRequest request)throws Exception {
        BaseResponseInfo res = new BaseResponseInfo();
        String lanCode = redisService.getLanCodeFromSession(request);
        try {
            List<DailyItem> dataList = new ArrayList<DailyItem>();
            if(!StringUtil.isEmpty(headerId)) {
                dataList = dailyItemService.getDetailList(headerId);
            }
            JSONObject outer = new JSONObject();
            outer.put("total", dataList.size());
            outer.put("rows", dataList);
            res.code = 200;
            res.data = outer;
        } catch (Exception e) {
            e.printStackTrace();
            res.code = 500;
            res.data = "zh".equals(lanCode) ? ErpInfo.FAILED.name : ErpInfo.FAILED.lanCodeName;
        }
        return res;
    }

    @GetMapping(value = "/getDetailListByTime")
    @ApiOperation(value = "生成明细")
    @ApiResponses({
            @ApiResponse(code = 200, message = "执行成功",response = DailyItem.class)
    })
    public BaseResponseInfo getDetailListByTime(@RequestParam("time") String time,
                                          HttpServletRequest request)throws Exception {
        BaseResponseInfo res = new BaseResponseInfo();
        String lanCode = redisService.getLanCodeFromSession(request);
        try {
            String beginTime = Tools.parseDayToTime(time, BusinessConstants.DAY_FIRST_TIME);
            String endTime = Tools.parseDayToTime(time, BusinessConstants.DAY_LAST_TIME);
            List<DailyItem>  dataList = dailyItemService.getDetailListByTime(beginTime,endTime);
            JSONObject outer = new JSONObject();
            outer.put("total", dataList.size());
            outer.put("rows", dataList);
            res.code = 200;
            res.data = outer;
        } catch (Exception e) {
            e.printStackTrace();
            res.code = 500;
            res.data = "zh".equals(lanCode) ? ErpInfo.FAILED.name : ErpInfo.FAILED.lanCodeName;
        }
        return res;
    }
}
